package top.melody.ecommerce.webapps.www.weblogic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.melody.ecommerce.common.util.JsonMapper;
import top.melody.ecommerce.common.util.security.DES3;
import top.melody.ecommerce.platform.EcommerceException;
import top.melody.ecommerce.platform.api.domain.account.Account;
import top.melody.ecommerce.platform.api.domain.account.AccountAddress;
import top.melody.ecommerce.platform.api.domain.account.AccountEnterpriseAuth;
import top.melody.ecommerce.platform.api.service.AccountAddressService;
import top.melody.ecommerce.platform.api.service.AccountAuthService;
import top.melody.ecommerce.platform.api.service.AccountService;
import top.melody.ecommerce.webapps.www.base.AssertContext;
import top.melody.ecommerce.webapps.www.base.Constant;
import top.melody.ecommerce.webapps.www.base.UserSession;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/11/16 14:51
 * @mail: blentle.huan.ren@gmail.com
 * @description:
 * @since:1.0
 */
@Service
public class AccountWeblogic {

    private final static Logger logger = LoggerFactory.getLogger(AccountWeblogic.class);
    @Resource
    private AccountService accountService;
    @Resource
    private AccountAuthService accountAuthService;
    @Resource
    private AccountAddressService accountAddressService;

    public Account signUpAccount(Account account, HttpServletRequest request) throws EcommerceException {
        Account returnAccount = accountService.signUp(account);
        UserSession userSession = new UserSession();
        userSession.setAccount(returnAccount);
        userSession.setPassword(account.getAccountPassword());
        AssertContext.putCurrentAccount2Session(userSession, request);
        return returnAccount;
    }


    public Account getAccount(String loginId) throws EcommerceException {
        return accountService.getAccount(loginId);
    }

    public Account signInAccount(Account account, String password, boolean rememberMe, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        UserSession userSession = new UserSession();
        userSession.setAccount(account);
        userSession.setPassword(password);
        //put current account into session
        AssertContext.putCurrentAccount2Session(userSession, request);
        if (rememberMe)
            //put current account into cookie
            AssertContext.putCurrentAccount2Cookie(userSession, 60 * 60 * 24 * 7, request, response);
        else
            //do not remember ,remove from cookie...
            AssertContext.removeFromCookie(Constant.COOKIE_ACCOUNT_KEY, request, response);
        return account;
    }

    public AccountEnterpriseAuth getAccountAuthByAccountId(int accountId) throws EcommerceException {
        return accountAuthService.getAccountEnterpriseAuthByAccountId(accountId);
    }

    public Account modifyAccountInfo(Account account, HttpServletRequest request,HttpServletResponse response) throws Exception {
        Account entity = accountService.updateAccount(account);
        UserSession userSession = new UserSession(entity, DES3.decode(entity.getAccountPassword()));
        AssertContext.putCurrentAccount2Session(userSession,request);
        AssertContext.putCurrentAccount2Cookie(userSession, 60 * 60 * 24 * 7, request, response);
        return entity;
    }

    public List<AccountAddress> findAccountAddressList(AccountAddress accountAddress) throws EcommerceException {
        return accountAddressService.findAccountAddressList(accountAddress);
    }
}
